package com.redis.data.seckillshopping.common;


import com.alibaba.fastjson.JSON;
import com.redis.data.seckillshopping.enums.RequestCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;


/**
 * @author 哲思小辉
 * @version 1.0.0
 * @ClassName Result.java
 * @Description 请求结果
 * @Param
 * @createTime 2020年03月24日 16:49:00
 */
@Setter
@Getter
@NoArgsConstructor
@ApiModel(value = "Result", description = "请求结果实体")
public class Result<T> {


    @ApiModelProperty(name = "code", value = "请求响应code", dataType = "String", example = "200")
    private int code = RequestCodeEnum.SUCCESS.code;

    @ApiModelProperty(name = "msg", value = "请求响应信息", dataType = "String", example = "请求成功")
    private String msg = RequestCodeEnum.SUCCESS.msg;

    @ApiModelProperty(name = "data")
    private T data;


    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(RequestCodeEnum requestCodeEnum) {
        this.code = requestCodeEnum.code;
        this.msg = requestCodeEnum.msg;
    }

    public Result(RequestCodeEnum requestCodeEnum, T data) {
        this.code = requestCodeEnum.code;
        this.msg = requestCodeEnum.msg;
        this.data = data;
    }

    public static Result success() {
        return new Result();
    }

    public static Result success(Object data) {
        return new Result(RequestCodeEnum.SUCCESS, data);
    }

    public static Result failure() {
        return new Result(RequestCodeEnum.FAILURE);
    }

    public static Result failure(Object data) {
        return new Result(RequestCodeEnum.FAILURE, data);
    }


    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
